package org.socialresume.client.mvp.view;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.socialresume.client.model.Feed;
import org.socialresume.client.model.FeedItem;
import org.socialresume.client.model.TwitterFeed;
import org.socialresume.client.mvp.view.widget.FeedWidget;
import org.socialresume.client.mvp.view.widget.TwitterFeedWidget;
import org.socialresume.client.mvp.view.widget.TwitterUpdateWidget;
import org.socialresume.client.mvp.view.widget.UpdateWidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class FeedsPanel extends SimplePanel{
	
	private static class FeedOverHandler implements MouseOverHandler{
		
		private static Label selectedLabel;
		
		private static void select(Label label){
			if(selectedLabel !=null){
				selectedLabel.removeStyleDependentName("selected");
			}
			selectedLabel = label;
			selectedLabel.addStyleDependentName("selected");
		}
		
		private FeedsPanel panel;
		
		public FeedOverHandler(FeedsPanel panel) {
			this.panel = panel;
		}
		
		@Override
		public void onMouseOver(MouseOverEvent event) {
			Label sourceLabel = (Label) event.getSource();
			select(sourceLabel);
			panel.showWidgetRelativeTo(sourceLabel.getText(),sourceLabel);
		}
	}

	private VerticalPanel feedsTitlePanel;
	private DeckPanel feedWidgetPanel;
	private Map<String, Widget> feedsRelativeToLabel;
	private Map<String, FeedWidget> feedsWidgets;
	private FeedWidget currentWidget;
	private FlexTable feedsWidegtContainer;
	private VerticalPanel feedUpdatesPanel;
	private Map<String,UpdateWidget> updates;
	private DecoratedPopupPanel popupPanel;
	private Widget loadingWidget;
	
	public FeedsPanel(boolean addUpdatesPanel) {
		super();
		popupPanel = new DecoratedPopupPanel(true);
		feedsTitlePanel = new VerticalPanel();
		feedUpdatesPanel = new VerticalPanel();
		ScrollPanel scrollPanel = new ScrollPanel(feedUpdatesPanel);
		HorizontalPanel loadingPanel = new HorizontalPanel();
		loadingPanel.add(new Image("public/loader.gif"));
		loadingPanel.add(new Label("Fetching feed update ..."));
		loadingWidget = loadingPanel;
		loadingWidget.setVisible(false);
		feedUpdatesPanel.add(loadingWidget);
		
		feedWidgetPanel = new DeckPanel();
		feedWidgetPanel.setAnimationEnabled(true);
		
		feedsWidgets = new HashMap<String, FeedWidget>();
		feedsRelativeToLabel = new HashMap<String, Widget>();
		
		feedsWidegtContainer = new FlexTable();
		feedsWidegtContainer.setWidth("100%");
		feedsWidegtContainer.setWidget(0, 0, feedsTitlePanel);
		VerticalPanel updatesVPanel = new VerticalPanel();
		updatesVPanel.add(loadingWidget);
		updatesVPanel.add(scrollPanel);
		if(addUpdatesPanel){
			feedsWidegtContainer.setWidget(0, 2, updatesVPanel);
			feedsWidegtContainer.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_LEFT);
			feedsWidegtContainer.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_TOP);
			feedsWidegtContainer.getCellFormatter().setStyleName(0, 2, "feedUpdatesCell");
		}
		feedsWidegtContainer.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		feedsWidegtContainer.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		feedsWidegtContainer.getCellFormatter().setStyleName(0, 0, "feedLabelCell");
		scrollPanel.setStyleName("updateContainer");
		feedWidgetPanel.setStyleName("feedWidget");
		loadingPanel.setStyleName("feedLoadingPanel");

//		feedsWidegtContainer.setWidget(0, 1, feedWidgetPanel);
//		feedsWidegtContainer.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
//		feedsWidegtContainer.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
//		feedsWidegtContainer.getCellFormatter().setStyleName(0, 1, "feedInfosCell");
		updates = new HashMap<String, UpdateWidget>();
		this.setWidget(feedsWidegtContainer);
		
		popupPanel.add(feedWidgetPanel);
		feedWidgetPanel.setVisible(false);
	}
	public void addFeed(TwitterFeed feed){
		TwitterFeedWidget widget = new TwitterFeedWidget(feed);
		addFeedWidget(widget);
	}
	
	public void addFeed(Feed feed){
		FeedWidget widget = new FeedWidget(feed);
		addFeedWidget(widget);
	}
	
	private void addFeedWidget(final FeedWidget feedWidget){
		Label feedLabel = new Label(feedWidget.getFeed().getTitle());
		feedLabel.setStyleName("feedLabel");
		feedLabel.setWordWrap(false);
		feedLabel.addMouseOverHandler(new FeedOverHandler(this));
		HorizontalPanel panel = new HorizontalPanel();
		SimplePanel relativeToPanel = new SimplePanel();
		
		panel.add(feedLabel);
		panel.add(relativeToPanel);
		this.feedsTitlePanel.add(panel);
		feedWidgetPanel.add(feedWidget);
		feedWidget.setLoadingWidget(loadingWidget);
		feedWidget.getFeed().addValueChangeHandler(new ValueChangeHandler<Collection<FeedItem>>() {
			@Override
			public void onValueChange(ValueChangeEvent<Collection<FeedItem>> event) {
				addUpdates(event.getValue());
//				loadingWidget.setVisible(false);
			}
		});
//		feedWidget.addSelectionHandler(new SelectionHandler<Feed>() {
//			@Override
//			public void onSelection(SelectionEvent<Feed> event) {
////				loadingWidget.setVisible(true);
//				feedWidget.update();
//			}
//			
//		});
		feedsWidgets.put(feedWidget.getFeed().getTitle(), feedWidget);
		feedsRelativeToLabel.put(feedWidget.getFeed().getTitle(), relativeToPanel);
	}
	
	private void showWidgetRelativeTo(String feed, Widget relativeToWidget){
		if(currentWidget != feedsWidgets.get(feed)){
			popupPanel.showRelativeTo(feedsRelativeToLabel.get(feed));
			feedWidgetPanel.setVisible(true);
//			feedUpdatesPanel.clear();
			currentWidget = feedsWidgets.get(feed);
			feedWidgetPanel.showWidget(feedWidgetPanel.getWidgetIndex(currentWidget));
		}
	}
	
	/*
	private void clearUpdates() {
		feedUpdatesPanel.clear();
		updates.clear();
	}
	*/

	private void addUpdates(Collection<FeedItem> value) {
		ArrayList<FeedItem> items = new ArrayList<FeedItem>();
		items.addAll(value);
		Collections.sort(items);
		if(items != null && !items.isEmpty()){
			GWT.log("Add updates "+items, null);
			for (FeedItem item : items) {
				if(!updates.containsKey(item.getId())){
					UpdateWidget widget = null;
					if(currentWidget instanceof TwitterFeedWidget){
						widget = new TwitterUpdateWidget((TwitterFeed) currentWidget.getFeed(), item);
					}else{
						widget = new UpdateWidget(currentWidget.getFeed(), item);
					}
					
					updates.put(item.getId(), widget);
					feedUpdatesPanel.add(widget);
				}
			}
		}else{
			GWT.log("No updates",null);
			feedUpdatesPanel.add(new Label("No updates"));
		}
	}
	
	public Map<String, FeedWidget> getFeedsWidgets() {
		return feedsWidgets;
	}
}
